var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
      coupon_name:'请选择',
      coupon_value:'',
      iscouponShow:false,
      freight:'0.00',
      isHasCoupon:true,
      coupon:'',
      totalPrice:'89.00',
      textValue:'',
    },
    linkAddress:function(){
      var that = this;
      wx.redirectTo({
            url: '/pages/address_administration/address_administration?flag=chose&source='+that.data.source+'&ids='+that.data.ids,
        })
    }, 
    linkCoupon:function(){
        var that=this;
        if(!that.data.isHasCoupon){
            wx.showToast({
                title: '暂无相关优惠券',
                image:'/img/fail.png',
                duration:1500
            })
        }else{
            wx.navigateTo({
                url: '/pages/coupon/coupon',
            })
        }
    },
    linkOrderDetail:function(){
      var that = this;
      wx.request({
        url: app.siteInfo.shoppath + 'create_order',
        method: "post",
        data: {
          id: that.data.ids,
          pid: app.siteInfo.pid,
          uid: app.siteInfo.uid,
          com_part_id: app.siteInfo.com_part_id,
          couponid: that.data.couponid,
          addrid:that.data.addrid,
          expmoney:that.data.expmoney,
          totalprice:that.data.totalPrice,
          trueprice:that.data.truePrice,
          remark:that.data.textValue,
          source:that.data.source
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.request({
              url: app.siteInfo.shoppath + 'payorder',
              data: {
                orderid: res.data.orderid,
                pid: app.siteInfo.pid,
                uid: app.siteInfo.uid,
                com_part_id: app.siteInfo.com_part_id
              },
              method: 'post',
              success: function (res) {
                console.log(res);
                // return;
                wx.requestPayment({
                  'appId': res.data.appId,
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr,
                  'package': res.data.package,
                  'signType': res.data.signType,
                  'paySign': res.data.paySign,
                  'success': function (res) {
                    console.log(res);
                    
                  },
                  'fail': function (res) {
                    console.log(res);
                  },
                  complete:function(){
                    wx.navigateTo({
                      url: '/pages/order_list/order_list',
                    })
                  }
                })
              }
            })
            
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
          }

        }
      })
       
    },
    getTextValue:function(e){
        var that=this;
        that.setData({
            textValue:e.detail.value,
        })
    },
  couponShow:function(){
    var that=this;
    if(that.data.iscouponShow){
      that.setData({
        iscouponShow:false,
      })
    }else{
      that.setData({
        iscouponShow:true,
      })
    }
  },
  noUse:function(){
    var that=this;
    that.setData({
      coupon_name: '',
      coupon_value: '',
      iscouponShow:false,
      truePrice:that.data.totalPrice
    })
  },
  chooseCoupon:function(e){
    var index=e.currentTarget.id;
    var that=this;
    var coupon=that.data.coupon;
    var totalprice = that.data.totalPrice - coupon[index].deduct;
    that.setData({
      coupon_name: coupon[index].couponname,
      coupon_value:coupon[index].deduct,
      iscouponShow:false,
      truePrice: totalprice,
      couponid: coupon[index].id,

    })
  },
  // 获取formid
  getFormId: function (e) {
    var formid = e.detail.formId;
    wx.request({
      url: app.siteInfo.siteroot + 'saveformid',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        formid: formid
      },
      success: function (res) {

      }
    })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this;
      var ids = options.ids;
      var source = options.source;
      var addrid = options.addrid ? options.addrid:0;
      that.setData({
        ids:ids,
        source: source,
        addrid: addrid,
      })
      
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that = this;
      var addrid = that.data.addrid;
      var ids = that.data.ids;
      var source = that.data.source;
      wx.request({
        url: app.siteInfo.shoppath + 'pre_order_xq',
        data: {
          uid: app.siteInfo.uid,
          pid: app.siteInfo.pid,
          com_part_id: app.siteInfo.com_part_id,
          addrid: addrid,
          source: source,
          ids: ids,
        },
        method: "post",
        success: function (res) {
          if (res.data.code == 200) {
            if (res.data.address) {
              that.setData({
                isHasDefaultAddress: true,
                addrid:res.data.address.id
              })
            }
            that.setData({
              goods: res.data.goods,
              address: res.data.address,
              coupon: res.data.coupon,
              totalPrice: res.data.totalprice,
              truePrice:res.data.totalprice,
              freight: res.data.expmoney,
            })
          } else {
            wx.showModal({
              title: '返回上一级',
              content: res.data.msg,
              complete:function(){
                wx.navigateTo({
                  url: '/pages/product/product',
                })
              }
            })
           
          }
        }
      })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {
    wx.request({
      url: app.siteInfo.siteroot + 'relay',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        replaysource: "index"
      },
      success: function (res) { }
    })
    return {
      title: app.siteInfo.sharetitle,
      path: "/pages/index/index?share_card_id=" + app.siteInfo.pid + "&shareid=" + app.siteInfo.uid,
      success: function (a) {
        wx.request({
          url: app.siteInfo.siteroot + 'relay',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid
          },
          success: function (res) { }
        })
      },
      fail: function (a) { }
    };
  }
})